package com.networkmanagerj
{
	
	import com.networkmanagerj.IDispose;
	
	import flash.display.Sprite;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;
	import mx.managers.ISystemManager;
	import mx.managers.PopUpManager;

	public class XvicAlert implements IDispose
	{
		public static const ALERT_BUTTON_OK : uint = 1;
		public static const ALERT_BUTTON_CANCEL : uint = 2;
		public static const ALERT_BUTTON_OK_CANCEL : uint = 3;
		public static const ALERT_BUTTON_YES_NO : uint = 4;
		
		public static const ALERT_ERROR : uint = 1;
		public static const ALERT_WARNING : uint = 2;
		public static const ALERT_HELP: uint = 3;
		public static const ALERT_INFO : uint = 4;
		public static const ALERT_DONE : uint = 5;
		
		
		
		[Embed(source='assets/images/alertError.png')]
		private static var iconError : Class;
		[Embed(source='assets/images/alertHelp.png')]
		private static  var iconHelp : Class;
		[Embed(source="assets/images/alertInfo.png")]
		private static  var iconInfo : Class;
		[Embed(source="assets/images/alertOK.png")]
		private static  var iconOK : Class;
		[Embed(source="assets/images/alertWarning.png")]
		private static  var iconWarning : Class;	
	
		private static var alert : Alert;
		private static var parent : Sprite = null;
				
		private static function createAlert(alertText : String, alertTitle : String, iconClass : Class,buttonsType : uint, closeFunction : Function):void
		{
			parent = null;
			alert = new Alert();
			alert.text = alertText;
			alert.title = alertTitle;
			alert.iconClass = iconClass;
			
			Alert.okLabel = "OK";
			Alert.yesLabel = "Sim";
			Alert.noLabel = "Não";
			Alert.cancelLabel = "Cancelar";
			
			if(closeFunction != null)
			   alert.addEventListener(CloseEvent.CLOSE,closeFunction);   
			
			switch (buttonsType)
			{
				case ALERT_BUTTON_OK        : alert.buttonFlags = Alert.OK;break;
				case ALERT_BUTTON_CANCEL    : alert.buttonFlags = Alert.CANCEL;break;
				case ALERT_BUTTON_OK_CANCEL : alert.buttonFlags = Alert.OK + Alert.CANCEL;break;
				case ALERT_BUTTON_YES_NO    : alert.buttonFlags = Alert.YES + Alert.NO;break;				
			}
			
//		   if(parent == null)
//		   {
//		      var sm : ISystemManager = ISystemManager(FlexGlobals.topLevelApplication); 
//		      if(sm.isTopLevel()){
//		         parent = Sprite(sm.getSandboxRoot());
//			  }else{
//		         parent = Sprite(FlexGlobals.topLevelApplication);
//			  }
//		   }			
		}
		
		
		public function XvicAlert()
		{}
		
		
		public static function showErrorAlert(alertText : String, alertTitle : String,buttonsType : uint = ALERT_BUTTON_OK,closeFunction : Function = null):void
		{
		   createAlert(alertText,alertTitle,iconError,buttonsType,closeFunction);		      
		   PopUpManager.addPopUp(alert,parent,true);
		   PopUpManager.centerPopUp(alert);
		               	
		}
		
		public static function showHelpAlert(alertText : String, alertTitle : String,buttonsType : uint = ALERT_BUTTON_OK,closeFunction : Function = null):void
	    {
		   createAlert(alertText,alertTitle,iconHelp,buttonsType,closeFunction);		      
		   PopUpManager.addPopUp(alert,parent,true);
		   PopUpManager.centerPopUp(alert);	    	
	    }
	    
	    public static function showInfoAlert(alertText : String, alertTitle : String,buttonsType : uint = ALERT_BUTTON_OK,closeFunction : Function = null):void
	    {
		   createAlert(alertText,alertTitle,iconInfo,buttonsType,closeFunction);		      
		   PopUpManager.addPopUp(alert,parent,true);
		   PopUpManager.centerPopUp(alert);	    	
	    }    
	    
	    public static function showWarningAlert(alertText : String, alertTitle : String,buttonsType : uint = ALERT_BUTTON_OK,closeFunction : Function = null):void
	    {
		   createAlert(alertText,alertTitle,iconWarning,buttonsType,closeFunction);		      
		   PopUpManager.addPopUp(alert,parent,true);
		   PopUpManager.centerPopUp(alert);	   	    	
	    }
	    
	    public static function showAlert(alertText : String, alertTitle : String,alertType : uint,buttonsType : uint = ALERT_BUTTON_OK,closeFunction : Function = null):void
	    {
	    	switch(alertType)
	    	{
	    		case ALERT_ERROR:
	    		{
	    			showErrorAlert(alertText,alertTitle,buttonsType,closeFunction);
	    			break;
	    		}
	    		
	    		case ALERT_HELP:
	    		{
	    			showHelpAlert(alertText,alertTitle,buttonsType,closeFunction);
	    			break;
	    		}
	    		
	    		case ALERT_INFO:
	    		{
	    			showInfoAlert(alertText,alertTitle,buttonsType,closeFunction);
	    			break;
	    		}
	    		
	    		case ALERT_WARNING:
	    		{
	    			showWarningAlert(alertText,alertTitle,buttonsType,closeFunction);
	    			break;
	    		}
	    		
	    		case ALERT_DONE:
	    		{
		   			createAlert(alertText,alertTitle,iconOK,buttonsType,closeFunction);		      
		   			PopUpManager.addPopUp(alert,parent,true);
		   			PopUpManager.centerPopUp(alert);
	    		}
	    	}
	    }	    
	    
	    public function dispose():void
	    {
	    }		
	}
}